package com.dingreading.cloud.app.controller;

import com.dingreading.cloud.app.entity.Corporations;
import com.dingreading.cloud.app.entity.Members;
import com.dingreading.cloud.app.entity.Stores;
import com.dingreading.cloud.app.service.CorporationsService;
import com.dingreading.cloud.app.service.MembersService;
import com.dingreading.cloud.app.util.UidUtil;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/app/members")
@Api(tags = "APP-会员信息管理")
public class AppMembersController {

    @Resource
    private CorporationsService corporationsService;
    @Resource
    private MembersService membersService;

    @ApiOperation(value = "app会员分页")
    @PostMapping(value = "/pageList")
    public R<Object> pageList(
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "真实全名") @RequestParam(value = "fullName", required = false) String fullName,
            @ApiParam(value = "公司名") @RequestParam(value = "companyName", required = false) String companyName,
            @ApiParam(value = "行政地区ID") @RequestParam(value = "divisionId", required = false) String divisionId,
            @ApiParam(value = "母公司ID") @RequestParam(value = "parentId", required = false) String parentId,
            @ApiParam(value = "筛选开始时间 yyyy-MM-dd") @RequestParam(value = "startTime", required = false) String startTime,
            @ApiParam(value = "筛选结束时间 yyyy-MM-dd") @RequestParam(value = "endTime", required = false) String endTime
    ) throws Exception {
//        Object store = UidUtil.getStore(storeUid);
//        if (store instanceof String) return R.fail(store.toString());
//        Stores stores = (Stores) store;
//
//        Page<Members> page = this.membersService.appPage(pageUtil, stores.getId(), fullName, startTime, endTime);
//        long total = page.getTotalRow();
//        List<Members> list = page.getRecords();

        return R.fail("功能已暂停，请升级App版本");
    }

    @ApiOperation(value = "app会员分页")
    @PostMapping(value = "/v2/pageList")
    public R<Object> pageListV2(
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "手机系统 1.安卓 2.ios") @RequestParam(value = "os") Integer os,
            @ApiParam(value = "设备标识") @RequestParam(value = "deviceId") String deviceId,
            @ApiParam(value = "版本号") @RequestParam(value = "versionCode") Integer versionCode,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "真实全名") @RequestParam(value = "fullName", required = false) String fullName,
            @ApiParam(value = "公司名") @RequestParam(value = "companyName", required = false) String companyName,
            @ApiParam(value = "行政地区ID") @RequestParam(value = "divisionId", required = false) String divisionId,
            @ApiParam(value = "母公司ID") @RequestParam(value = "parentId", required = false) String parentId,
            @ApiParam(value = "筛选开始时间 yyyy-MM-dd") @RequestParam(value = "startTime", required = false) String startTime,
            @ApiParam(value = "筛选结束时间 yyyy-MM-dd") @RequestParam(value = "endTime", required = false) String endTime
    ) throws Exception {
        Object store = UidUtil.getStore(storeUid);
        if (store instanceof String)
            return R.fail(store.toString());
        Stores stores = (Stores) store;

        Corporations corporations = corporationsService.getByUid(stores.getCorpUid());
        String judgeCorp = UidUtil.judgeCorp(corporations);
        if (StringUtils.isNotBlank(judgeCorp))
            return R.fail(judgeCorp);

        Page<Members> page = this.membersService.appPage(pageUtil, stores.getId(), fullName, startTime, endTime);
        long total = page.getTotalRow();
        List<Members> list = page.getRecords();

        return R.ok(list);
    }
}